function [prof_norm, scale_factor] = profnorm(prof_data, prof_ref);

% PROFNORM - normalize profiles
%
%    [NORM, SCALE] = PROFNORM(DATA, REF) normalizes the profiles in DATA
%    to the profile in REF and returns the scaled profiles to NORM and
%    their corresponding scaling factors to SCALE.

% By:   S.C. Molitor (smolitor@med.unc.edu)
% Date: May 31, 2000

% initialize output

prof_norm = [];
scale_factor = [];

% validate arguments

if (nargin ~= 2)
   msgbox('Invalid number of arguments', 'PROFNORM Error', 'warn');
   return
elseif (~isnumeric(prof_data) | isempty(prof_data))
   msgbox('DATA must be a numeric array', 'PROFNORM Error', 'warn');
   return
elseif (~isnumeric(prof_ref) | isempty(prof_ref))
   msgbox('REF must be a numeric array', 'PROFNORM Error', 'warn');
   return
end

% first column of output is first column of data

prof_norm = prof_data(:, 1);

% normalize data columns

for i = 2 : size(prof_data, 2)
   scale_factor(i - 1) = sum(prof_data(:, i) .* prof_ref(:, end)) / sum(prof_data(:, i).^2);
   prof_norm(:, i) = scale_factor(i - 1) * prof_data(:, i);
end
return
